using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace CDTM.Modules.ONLINE.Components
{
    public partial class Online
    {

        //---------------------------------------------------------------------------------------------

        public static List<Online> SelectCollectionBy(Guid moduleID, int categoryID)
        {
            List<Online> collection = new List<Online>();
            SqlDataReader reader = (SqlDataReader)SelectReaderAll(moduleID, categoryID);
            while (reader.Read())
            {
                Online entity = new Online();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiHoTro"))) entity.NoiHoTro = reader.GetString(reader.GetOrdinal("NoiHoTro"));
                if (!reader.IsDBNull(reader.GetOrdinal("NickName"))) entity.NickName = reader.GetString(reader.GetOrdinal("NickName"));
                if (!reader.IsDBNull(reader.GetOrdinal("NickType"))) entity.NickType = reader.GetString(reader.GetOrdinal("NickType"));
                if (!reader.IsDBNull(reader.GetOrdinal("HoTen"))) entity.HoTen = reader.GetString(reader.GetOrdinal("HoTen"));
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) entity.SortOrder = reader.GetInt32(reader.GetOrdinal("SortOrder"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThai"))) entity.TrangThai = reader.GetBoolean(reader.GetOrdinal("TrangThai"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll(Guid moduleID, int categoryID)
        {
            const string spName = "[dbo].[p_CDTM_Online_Item_SelectBy_ID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);
            return db.ExecuteReader(dbCommand);
        }
    }
}